package com.tnovoselec.android.common.ui.toast;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tnovoselec.android.common.R;


public class ImageToast extends Toast {

    private ImageToast(Activity activity, int imageResource, int textResource, int duration) {
	this(activity, imageResource, activity.getResources().getString(textResource), duration);
    }

    private ImageToast(Activity activity, int imageResource, String text, int duration) {
	super(activity);

	View layout = activity.getLayoutInflater().inflate(R.layout.toast, (ViewGroup) activity.findViewById(R.id.toast_layout_root));

	((ImageView) layout.findViewById(R.id.image)).setImageResource(imageResource);
	((TextView) layout.findViewById(R.id.text)).setText(text);

	super.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
	super.setDuration(duration);
	super.setView(layout);
    }

    /**
     * Creates instance of ImageToast with id of String resource, and id of
     * image resource
     * 
     * @param activity
     * @param imageResource
     * @param textResource
     * @return ImageToast instance
     */
    public static ImageToast make(Activity activity, int imageResource, int textResource) {
	return new ImageToast(activity, imageResource, textResource, Toast.LENGTH_SHORT);
    }

    /**
     * Creates instance of ImageToast with custom text, and id of image
     * resource. The duration is default Toast.LENGTH_SHORT
     * 
     * @param activity
     * @param imageResource
     * @param textResource
     * @return ImageToast instance
     */
    public static ImageToast make(Activity activity, int imageResource, String text) {
	return new ImageToast(activity, imageResource, text, Toast.LENGTH_SHORT);
    }

    /**
     * Shows instance of ImageToast with id of String resource, and id of image
     * resource. The duration is default Toast.LENGTH_SHORT
     * 
     * @param activity
     * @param imageResource
     * @param textResource
     */
    public static void show(Activity activity, int imageResource, int textResource) {
	(new ImageToast(activity, imageResource, textResource, Toast.LENGTH_SHORT)).show();
    }

    /**
     * Shows instance of ImageToast with custom text, and id of image resource.
     * The duration is default Toast.LENGTH_SHORT
     * 
     * @param activity
     * @param imageResource
     * @param textResource
     */
    public static void show(Activity activity, int imageResource, String text) {
	(new ImageToast(activity, imageResource, text, Toast.LENGTH_SHORT)).show();
    }

    /**
     * Shows instance of ImageToast with custom text, id of image resource and
     * desired duration.
     * 
     * @param activity
     * @param imageResource
     * @param textResource
     */
    public static void show(Activity activity, int imageResource, int textResource, int duration) {
	(new ImageToast(activity, imageResource, textResource, duration)).show();
    }
}
